﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHose.Application.allots.Dto;
using WareHose.Application.TaskStocks.DTO;
using WareHouse.Domain;
using WareHouse.Repositories;

namespace WareHose.Application.allots
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v2")]
    public class Alootsservice:IDynamicWebApi
    {
        //仓储
        private readonly IBaseRepository<allot> db;
        //AutoMapper
        private readonly IMapper mapper;
        private readonly ISqlSugarClient sqlSugarClient;


        public Alootsservice(IBaseRepository<allot> db, IMapper mapper, ISqlSugarClient sqlSugarClient)
        {
            this.db = db;
            this.mapper = mapper;
            this.sqlSugarClient = sqlSugarClient;
        }




        /// <summary>
        /// 调拨添加
        /// </summary>
        /// <param name = "RoleInputDTo" ></ param >
        /// < returns ></ returns >

        [HttpPost]
        [AllowAnonymous]
        public async Task<ResultDto> AddUpdAsync(AllotsDto AllotsDto)
        {
            try
            {

                var entity = mapper.Map<AllotsDto, allot>(AllotsDto);

                await db.InsertAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "添加成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }



        /// <summary>
        ///调拨显示
        /// </summary>
        /// <param name="RoleRequestDTo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultDtos<List<AllotsDto>>> AdminPage(string? allotNumber, DateTime? AddDate, int State, int pageIndex = 1, int pageSize = 4)
        {

            var list = (from a in await db.QueryAsync()
                        select new AllotsDto
                        {
                            allotId = a.allotId,
                            allotNumber = a.allotNumber,
                            allotName = a.allotName,
                            batch = a.batch,
                            Specification = a.Specification,
                            NUmber = a.NUmber,
                            allotOdd=a.allotOdd,
                            Cang = a.Cang,
                            Che = a.Che,
                            SpecificationCang = a.SpecificationCang,
                            State = a.State,
                            UpdPerson = a.UpdPerson,
                            UpdDate = a.UpdDate,
                        });
            if (!string.IsNullOrEmpty(allotNumber))
            {
                list = list.Where(m => m.allotNumber.Contains(allotNumber));
            }
            if (AddDate != null)
            {
                list = list.Where(m => m.UpdDate >= AddDate);
            }
            if (State != 0)
            {
                list = list.Where(m => m.State == State);
            }
           
            int listcount = list.Count();
            list = list.OrderBy(x => x.allotId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();


           

            return new ResultDtos<List<AllotsDto>>
            {
                Code = Status.Success,
                Data = list.ToList(),
                Listcount = listcount,
            };
        }



        /// <summary>
        /// 调拨批量删除 
        /// </summary>
        /// <param name="RoldId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultDto> DeleRoleAsync(string allotId)
        {
            if (allotId != null)
            {
                string[] ids = allotId.Split(',');
                foreach (var item in ids)
                {
                    await db.DeleAsync(Convert.ToInt64(item));
                }
                return new ResultDto { Code = Status.Success, Message = "删除成功" };
            }
            else
            {
                return new ResultDto { Code = Status.Success, Message = "删除失败" };
            }

        }







        /// <summary>
        /// 调拨修改
        /// </summary>
        /// <param name="RoleInputDTo"></param>
        /// <returns></returns>

        [HttpPut]
        public async Task<ResultDto> RoleUpdateAsync(AllotsDto AllotsDto)
        {
            try
            {

                var entity = mapper.Map<AllotsDto, allot>(AllotsDto);
                await db.UpdateAsync(entity);
                return new ResultDto { Code = Status.Success, Message = "修改成功" };

            }
            catch (Exception e)
            {
                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }


        /// <summary>
        /// 调拨反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ResultDto<allot>> BackFill(long id)
        {
            var list = await db.FistAsync(x => x.allotId == id);

            return new ResultDto<allot> { Code = Status.Success, Message = "成功", Data = list };
        }
    }
}
